package com.qiyi.player.wonder.plugins.scenetile.view.greentailpart
{
   import flash.display.Sprite;
   import scenetile.GreenTailTunkMC;
   import scenetile.GreenTailBitmap;
   import flash.text.TextField;
   import com.qiyi.player.wonder.common.ui.SimpleBtn;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.text.TextFieldAutoSize;
   import flash.events.MouseEvent;
   import flash.events.TextEvent;
   import gs.TweenLite;
   import com.qiyi.player.wonder.plugins.scenetile.view.SceneTileEvent;
   import flash.net.navigateToURL;
   import flash.net.URLRequest;
   import com.iqiyi.components.global.GlobalStage;
   import com.qiyi.player.wonder.plugins.scenetile.SceneTileDef;
   import com.qiyi.player.wonder.common.utils.StringUtils;
   import flash.net.URLLoader;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.qiyi.player.wonder.common.config.SystemConfig;
   import com.adobe.serialization.json.JSON;
   
   public class TunkVersionPanel extends Sprite
   {
      
      public function TunkVersionPanel() {
         super();
         this.initTunkPanel();
      }
      
      private static const PANEL_W:Number = 400;
      
      private static const REQUEST_TIMES:uint = 5;
      
      private static const DEF_TITLE_STRING:String = "绿尾巴敲敲，精彩随心看";
      
      private static const DEF_WARN_REST:String = "人太多了,休息,休息一下...";
      
      private static const DEF_WARN_ERROR:String = "请用图中建议的方法来碰哦";
      
      private static const DEF_PAIRING:String = "正在努力搜索设备，请稍候...";
      
      private static const DEF_CUT_CONNECT:String = "连接中断，重新碰下试试吧";
      
      private static const TEXT_CLIP_DESCRIBE_1:String = "1.打开爱奇艺视频APP\n   没安装   <a href=\'event:onTextEventClick\'>" + "<font color=\'#99cc33\'><u>马上下载</u></font></a>";
      
      private static const TEXT_CLIP_DESCRIBE_2:String = "2.点击App播放器中\n   右上角的绿尾巴";
      
      private static const TEXT_CLIP_DESCRIBE_3:String = "3.用手机敲击空格键";
      
      private var _greenTailMC:GreenTailTunkMC;
      
      private var _greenTailBitmap:GreenTailBitmap;
      
      private var _tfGreenTailHint:TextField;
      
      private var _againCollideBtn:SimpleBtn;
      
      private var _instantLookBtn:SimpleBtn;
      
      private var _instantPassonBtn:SimpleBtn;
      
      private var _returnLookBtn:SimpleBtn;
      
      private var _cutConnectBtn:SimpleBtn;
      
      private var _titleTF:TextField;
      
      private var _clipDescribeTF1:TextField;
      
      private var _clipDescribeTF2:TextField;
      
      private var _clipDescribeTF3:TextField;
      
      private var _uuid:String;
      
      private var _pcVideoName:String;
      
      private var _phoneVideoName:String;
      
      private var _jsonData:Object;
      
      private var _timeAverageDifference:Number = 0;
      
      private var _currentTimeNum:uint = 0;
      
      private var _currentMateTimeNum:uint = 0;
      
      private var _currentPairNum:uint = 0;
      
      private var _isPair:Boolean = false;
      
      private function initTunkPanel() : void {
         this._greenTailMC = new GreenTailTunkMC();
         this._titleTF = FastCreator.createLabel(DEF_TITLE_STRING,16777215,20);
         this._titleTF.y = 14;
         this._titleTF.x = (PANEL_W - this._titleTF.width) / 2;
         this._greenTailMC.addChild(this._titleTF);
         this._clipDescribeTF1 = FastCreator.createLabel(TEXT_CLIP_DESCRIBE_1,16777215);
         this._clipDescribeTF1.x = 93;
         this._clipDescribeTF1.y = 80;
         this._greenTailMC.addChild(this._clipDescribeTF1);
         this._clipDescribeTF2 = FastCreator.createLabel(TEXT_CLIP_DESCRIBE_2,16777215);
         this._clipDescribeTF2.x = 25;
         this._clipDescribeTF2.y = 146;
         this._greenTailMC.addChild(this._clipDescribeTF2);
         this._clipDescribeTF3 = FastCreator.createLabel(TEXT_CLIP_DESCRIBE_3,16777215);
         this._clipDescribeTF3.x = 245;
         this._clipDescribeTF3.y = 155;
         this._greenTailMC.addChild(this._clipDescribeTF3);
         addChild(this._greenTailMC);
         this._greenTailBitmap = new GreenTailBitmap();
         this._greenTailBitmap.x = PANEL_W / 2;
         this._greenTailBitmap.y = 44;
         addChild(this._greenTailBitmap);
         this._tfGreenTailHint = FastCreator.createLabel("",16777215,18,TextFieldAutoSize.CENTER);
         this._tfGreenTailHint.y = this._greenTailBitmap.y + 32;
         addChild(this._tfGreenTailHint);
         this._againCollideBtn = new SimpleBtn("重新发送");
         this._againCollideBtn.x = (PANEL_W - this._againCollideBtn.width) * 0.5;
         addChild(this._againCollideBtn);
         this._instantPassonBtn = new SimpleBtn("立即传输");
         this._instantPassonBtn.x = PANEL_W * 0.25 - this._instantPassonBtn.width * 0.5;
         addChild(this._instantPassonBtn);
         this._cutConnectBtn = new SimpleBtn("断开连接");
         addChild(this._cutConnectBtn);
         this._instantLookBtn = new SimpleBtn("立即观看");
         this._instantLookBtn.x = PANEL_W * 0.25 - this._instantLookBtn.width * 0.5;
         addChild(this._instantLookBtn);
         this._returnLookBtn = new SimpleBtn("返回观看");
         this._returnLookBtn.x = PANEL_W * 0.75 - this._returnLookBtn.width * 0.5;
         addChild(this._returnLookBtn);
         this._againCollideBtn.y = this._instantPassonBtn.y = this._instantLookBtn.y = this._returnLookBtn.y = this._cutConnectBtn.y = 120;
         this._againCollideBtn.addEventListener(MouseEvent.CLICK,this.onAgainCollideBtnClick);
         this._instantPassonBtn.addEventListener(MouseEvent.CLICK,this.onInstantPassonBtnClick);
         this._instantLookBtn.addEventListener(MouseEvent.CLICK,this.onInstantLookBtnClick);
         this._cutConnectBtn.addEventListener(MouseEvent.CLICK,this.onCutConnectBtnClick);
         this._clipDescribeTF1.addEventListener(TextEvent.LINK,this.onLinkTextClick);
         this._returnLookBtn.addEventListener(MouseEvent.CLICK,this.onReturnLookBtnClick);
      }
      
      public function openPanel() : void {
         this._currentTimeNum = 0;
         this._currentMateTimeNum = 0;
         this._currentPairNum = 0;
         this._timeAverageDifference = 0;
         this._phoneVideoName = "";
         this.requestTimeSynchro();
      }
      
      public function closePanel() : void {
         this._currentMateTimeNum = this._currentPairNum = 10;
         TweenLite.killTweensOf(this.onClosePanel);
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         TweenLite.killTweensOf(this.requestMateInfo);
         this._isPair = false;
         this.hideChildren();
      }
      
      public function get isPair() : Boolean {
         return this._isPair;
      }
      
      private function onReturnLookBtnClick(param1:MouseEvent) : void {
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_ReturnLookBtnClick));
      }
      
      private function onLinkTextClick(param1:TextEvent) : void {
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_LinkTextClick));
      }
      
      private function onAgainCollideBtnClick(param1:MouseEvent) : void {
         this.showDefaultPanel(DEF_TITLE_STRING,true);
      }
      
      private function onInstantPassonBtnClick(param1:MouseEvent = null) : void {
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         if(this._phoneVideoName == "")
         {
            this.showOutputSuccessPanel(this._pcVideoName);
         }
         else
         {
            this.showReceiveSuccessPanel(this._phoneVideoName);
         }
      }
      
      private function onInstantLookBtnClick(param1:MouseEvent) : void {
         var _loc2_:String = null;
         if(this._jsonData != null)
         {
            _loc2_ = this._jsonData.videoUrl + "?s=" + Math.ceil(this._jsonData.videoPlayTime / 1000).toString();
            navigateToURL(new URLRequest(_loc2_),"_blank");
         }
         if(GlobalStage.isFullScreen())
         {
            GlobalStage.setNormalScreen();
         }
      }
      
      private function onCutConnectBtnClick(param1:MouseEvent) : void {
         TweenLite.killTweensOf(this.onClosePanel);
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         this.showDefaultPanel(DEF_TITLE_STRING,true);
      }
      
      private function hideChildren() : void {
         this._returnLookBtn.visible = this._instantLookBtn.visible = this._cutConnectBtn.visible = this._instantPassonBtn.visible = this._againCollideBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = this._greenTailMC.visible = false;
      }
      
      private function showDefaultPanel(param1:String, param2:Boolean = false) : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_INIT));
         this._greenTailMC.visible = true;
         this._titleTF.htmlText = param1;
         if(param2)
         {
            this._greenTailMC.play();
         }
      }
      
      private function showConnectErrorPanel() : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_NONE));
         this._againCollideBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = true;
         this._tfGreenTailHint.htmlText = DEF_CUT_CONNECT;
         this._tfGreenTailHint.x = (PANEL_W - this._tfGreenTailHint.width) / 2;
      }
      
      private function showConnectSuccessPanel(param1:String) : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_NONE));
         this._cutConnectBtn.visible = this._instantPassonBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = true;
         this._cutConnectBtn.x = PANEL_W * 0.75 - this._cutConnectBtn.width * 0.5;
         var _loc2_:* = "您已和" + param1 + "的手机连接";
         this._tfGreenTailHint.htmlText = StringUtils.remainWord(_loc2_,18);
         this._tfGreenTailHint.x = (PANEL_W - this._tfGreenTailHint.width) / 2;
         TweenLite.delayedCall(5,this.onInstantPassonBtnClick);
      }
      
      private function showOutputSuccessPanel(param1:String) : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_NONE));
         this._cutConnectBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = true;
         this._cutConnectBtn.x = PANEL_W * 0.5 - this._cutConnectBtn.width * 0.5;
         this._tfGreenTailHint.htmlText = StringUtils.remainWord(param1,18);
         this._tfGreenTailHint.x = (PANEL_W - this._tfGreenTailHint.width) / 2;
         TweenLite.delayedCall(3,this.onClosePanel);
      }
      
      private function onClosePanel() : void {
         TweenLite.killTweensOf(this.onClosePanel);
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_GreenTailClose));
      }
      
      private function showReceiveSuccessPanel(param1:String) : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_NONE));
         this._instantLookBtn.visible = this._returnLookBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = true;
         this._tfGreenTailHint.htmlText = StringUtils.remainWord(param1,18);
         this._tfGreenTailHint.x = (PANEL_W - this._tfGreenTailHint.width) / 2;
      }
      
      private function requestTimeSynchro(param1:Boolean = false) : void {
         if(!param1)
         {
            this.showDefaultPanel(DEF_TITLE_STRING,true);
         }
         var _loc2_:URLLoader = new URLLoader();
         _loc2_.addEventListener(Event.COMPLETE,this.onRequestTimeSynchroComplete);
         _loc2_.addEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         _loc2_.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc3_:String = SystemConfig.TIME_SYNCHRO_URL + "?random=" + int(Math.random() * 10000);
         _loc2_.load(new URLRequest(_loc3_));
      }
      
      private function onRequestTimeSynchroComplete(param1:Event) : void {
         param1.target.removeEventListener(Event.COMPLETE,this.onRequestTimeSynchroComplete);
         param1.target.removeEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         param1.target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc2_:Object = com.adobe.serialization.json.JSON.decode(param1.target.data);
         if(this._currentTimeNum < REQUEST_TIMES - 1)
         {
            this._timeAverageDifference = this._timeAverageDifference + (Number(_loc2_.data.base_time) - new Date().time);
            this.requestTimeSynchro(true);
         }
         else
         {
            this._timeAverageDifference = this._timeAverageDifference + (Number(_loc2_.data.base_time) - new Date().time);
            this._timeAverageDifference = this._timeAverageDifference / REQUEST_TIMES;
         }
         this._currentTimeNum++;
      }
      
      public function requestBlankEvent(param1:String, param2:String, param3:int, param4:String, param5:String) : void {
         this._uuid = param1;
         this._pcVideoName = param5;
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         var _loc6_:URLLoader = new URLLoader();
         _loc6_.addEventListener(Event.COMPLETE,this.onRequestBlankEventComplete);
         _loc6_.addEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         _loc6_.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc7_:String = SystemConfig.BLANK_EVENT_URL + "?uuid=" + param1 + "&device_name=pc" + "&device_type=pc" + "&event_type=1" + "&event_time=" + (new Date().time + this._timeAverageDifference) + "&longitude=&latitude=&velocity=&flash=1" + "&tvid=" + param2 + "&videoPlayTime=" + param3 + "&albumId=" + param4 + "&videoName=" + encodeURIComponent(this._pcVideoName) + "&tn=" + Math.random();
         _loc6_.load(new URLRequest(_loc7_));
         this.showDefaultPanel(DEF_PAIRING);
      }
      
      private function onRequestBlankEventComplete(param1:Event) : void {
         param1.target.removeEventListener(Event.COMPLETE,this.onRequestBlankEventComplete);
         param1.target.removeEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         param1.target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc2_:Object = com.adobe.serialization.json.JSON.decode(param1.target.data);
         if(_loc2_.code == "A00000")
         {
            this._isPair = true;
            TweenLite.delayedCall(1,this.requestMateInfo);
         }
         else
         {
            this._isPair = false;
            this.showConnectErrorPanel();
         }
      }
      
      private function requestMateInfo() : void {
         TweenLite.killTweensOf(this.requestMateInfo);
         var _loc1_:URLLoader = new URLLoader();
         _loc1_.addEventListener(Event.COMPLETE,this.onRequestMateInfoComplete);
         _loc1_.addEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         _loc1_.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc2_:String = SystemConfig.MATE_INFO_URL + "?uuid=" + this._uuid + "&device_name=pc" + "&device_type=pc" + "&event_type=1" + "&tn=" + Math.random();
         _loc1_.load(new URLRequest(_loc2_));
         this._currentMateTimeNum++;
      }
      
      private function onRequestMateInfoComplete(param1:Event) : void {
         param1.target.removeEventListener(Event.COMPLETE,this.onRequestMateInfoComplete);
         param1.target.removeEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         param1.target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc2_:Object = com.adobe.serialization.json.JSON.decode(param1.target.data);
         if(_loc2_.code == "A00000")
         {
            this._jsonData = _loc2_.data;
            if(_loc2_.data == null)
            {
               this._phoneVideoName = "";
               this.showConnectSuccessPanel("null");
            }
            else
            {
               this.showConnectSuccessPanel(_loc2_.data.device_name);
               this._phoneVideoName = _loc2_.data.videoName;
            }
            this._isPair = false;
         }
         else if(this._currentMateTimeNum < REQUEST_TIMES)
         {
            TweenLite.delayedCall(1,this.requestMateInfo);
         }
         else
         {
            this._currentPairNum++;
            this._currentMateTimeNum = 0;
            if(this._currentPairNum >= REQUEST_TIMES)
            {
               this.showDefaultPanel(DEF_WARN_REST);
            }
            else
            {
               this.showDefaultPanel(DEF_WARN_ERROR);
            }
            this._isPair = false;
         }
         
      }
      
      private function onUrlError(param1:Event) : void {
         param1.target.removeEventListener(Event.COMPLETE,this.onRequestTimeSynchroComplete);
         param1.target.removeEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         param1.target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
      }
   }
}
